package com.zlm.common.enums;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum SexEnum {
	UNDEFINED(-1, "未知"), MALE(0, "女"), FEMALE(1, "男");

	private int code;
	private String name;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private SexEnum(int code, String name) {
		this.code = code;
		this.name = name;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<Map<String, Object>> getList() {
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> map;
		for (SexEnum value : SexEnum.values()) {
			map = new HashMap();
			map.put("code", value.getCode());
			map.put("name", value.getName());
			list.add(map);
		}
		return list;
	}

	public static String getJsonString() {
		StringBuilder sb = new StringBuilder("{");
		for (SexEnum value : SexEnum.values()) {
			sb.append("\"" + value.getCode() + "\":\"" + value.getName() + "\",");
		}
		String json = sb.substring(0, sb.length() - 1) + "}";
		return json;
	}

	public static String getNameByCode(int code) {
		for (SexEnum value : SexEnum.values()) {
			if (value.code == code) {
				return value.name;
			}
		}
		return "";
	}

	public static int getCodeByName(String name) {
		for (SexEnum value : SexEnum.values()) {
			if (value.name == name) {
				return value.code;
			}
		}
		return 1;
	}

	@Override
    public String toString() {
		return "[code=" + code + ", name=" + name + "]";
	}
}